package cn.xfdzx.android.apps.shop.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

public class WebViewActivity extends BaseMvpActivity {

    private static final String TAG = "WebViewActivity";
    @BindView(R.id.wb_view)
    WebView wbView;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @Override
    public int getLayoutId() {
        return R.layout.activity_web_view;
    }

    @Override
    public void initView() {



        tvTitle.setText(getIntent().getStringExtra("title"));

        WebSettings webSettings = wbView.getSettings();
        webSettings.setSaveFormData(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(false);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setBlockNetworkImage(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

        //加载的url按规定的使用即可
        wbView.addJavascriptInterface(new Test(), "android");
        wbView.loadUrl(getIntent().getStringExtra("url"));

//        wbView.setWebViewClient(new WebViewClient() {
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                wbView.loadUrl("javascript:onOpenPage('" + APP_TOKEN + "','" + "android" + "')");
//            }
//        });

    }


    boolean flag=true;
    @Override
    protected void onResume() {
        super.onResume();

        /**
         *  防止用户未登录进入此页面  去登录操作
         *  防止token  为传给js
         */
        if (!TextUtils.isEmpty(APP_TOKEN)) {
            if (flag){
                wbView.reload();
                flag=false;
            }
        }
        wbView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);

                    view.loadUrl("javascript:onOpenPage('" + APP_TOKEN + "','" + "android" + "')");
                }
            });

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected void onDestroy() {


        CookieSyncManager.createInstance(WebViewActivity.this);  //Create a singleton CookieSyncManager within a context
        CookieManager cookieManager = CookieManager.getInstance(); // the singleton CookieManager instance
        cookieManager.removeAllCookie();// Removes all cookies.
        CookieSyncManager.getInstance().sync(); // forces sync manager to sync now
        WebStorage.getInstance().deleteAllData();
        wbView.getSettings().setJavaScriptEnabled(false);

        wbView.setWebViewClient(null);
        wbView.setWebChromeClient(null);
        wbView.clearCache(true);
        wbView.clearFormData();
        wbView.clearMatches();
        wbView.clearHistory();
        wbView.clearView();
        wbView.destroy();

        wbView = null;

        super.onDestroy();


    }


    @OnClick({R.id.iv_back})
    public void onclick(View view) {
        switch (view.getId()) {

            case R.id.iv_back:
                finish();
                break;
        }
    }

    //
    class Test {
        /**
         * 网页使用的js，方法无参数
         */
        @JavascriptInterface
        public void startFunction() {
            Log.e("startFunction", "----哈哈哈哈无参");
        }


        @JavascriptInterface
        public void startFunction(String data) {
        }

        @JavascriptInterface
        public void gotoGoodsDetail(String goodsId) {


            Intent intent = new Intent(WebViewActivity.this, XFDGoodsDeatailsActivity.class);
            intent.putExtra("good_id", goodsId + "");
            startActivity(intent);
        }

        @JavascriptInterface
        public void shoppingCartBtnClick() {

            Intent intent = new Intent(WebViewActivity.this, GoodCarActivity.class);
            intent.putExtra("type", 1);
            startActivity(intent);
        }


        @JavascriptInterface
        public void loginAction() {

            Intent intent = new Intent(WebViewActivity.this, OneLoginMainActivity.class);
            startActivity(intent);


        }
    }



}
